import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateBannerDto } from './dto/create-banner.dto';
import { UpdateBannerDto } from './dto/update-banner.dto';
import { Bk_Banner } from './entities/bk_banner.entity';
import * as fs from 'fs';
import { join } from 'path';
import { v1 } from 'node-uuid';
@Injectable()

export class BannerService {

  constructor(
    @InjectRepository(Bk_Banner)
    private readonly bannerRepository: Repository<Bk_Banner>
  ) { }

  async create(files: any, createBannerDto: CreateBannerDto) {
    createBannerDto.img_src = v1() + '.' + files.banner[0].originalname.split('.')[1];
    fs.writeFileSync(join(__dirname, '..', 'public', 'banner', createBannerDto.img_src), files.banner[0].buffer);
    return await this.bannerRepository.save(createBannerDto);
  }

  findAll() {
    return this.bannerRepository.find({ order: { orderNumber: "ASC" } });
  }

  findOne(id: number) {
    return `This action returns a #${id} banner`;
  }

  update(id: number, updateBannerDto: UpdateBannerDto) {
    return `This action updates a #${id} banner`;
  }

  remove(id: number) {
    return `This action removes a #${id} banner`;
  }
}
